
/* App ************************************************************************************ */

var App = {
	
	config: {},
	
	init: function(config) {
    	jQuery.extend(this.Config.vars, config);
	},
	
	Config : {
		vars : {},
		get : function(key) {
			return this.vars[key];
		}
	}
};


/* Cookie ************************************************************************************ */

App.Cookie = {
	set : function (name, value, days) {
		if (typeof value == 'undefined') {
			var value 	= '';
			var days 	= -1;
		}
		var expires = '';
		if (typeof days != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = '; expires=' + date.toGMTString();
		}

		document.cookie = name + '=' + value + expires + '; path=/';
	},
	get : function(name) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for (var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return false;
	}
};

/* Script ************************************************************************************ */

App.Script = {
	loaded: {},
	load : function(script, afterLoad) {
		new App.Script.Loader(
			script.split(','),
			(typeof afterLoad != 'undefined') ? afterLoad : function() {} 
		);
	},
	Loader : Class.extend({
		init : function(scripts, afterLoad) {
			var self = this;
			this.afterLoad = afterLoad;
			this.scriptCounter = scripts.length;
			for (var i = 0 ; i < scripts.length ; i++) {
				script = scripts[i];
				if (!App.Script.loaded[script]) {
					App.Script.loaded[script] = true;
					jQuery.getScript(
						App.Config.get('baseURL') + 'assets/js/app/' + script + '.js', 
						function() {
							self.onLoad();
						}
					);
				} else {
					self.onLoad();
				}
			}
		},
		onLoad : function() {
			this.scriptCounter--;
			if (this.scriptCounter == 0) {
				this.afterLoad();
			}
		}
	})
};

	
/* Request ******************************************************************************* */	
	
App.Request = Class.extend({
	config : {
		url 	: '',
		output 	: 'raw',
		params	: {}
	},
	callback : function() {},
	init : function(url, params, callback, outputType, preloadContent) {
		
		this.config.url 	= App.Config.get('baseURL') + 'do' + url;
		this.config.params 	= params;
		
		if (typeof outputType != 'undefined') {
			this.config.url += '?output=' + outputType;
			this.config.output = outputType;
		}
		
		this.callback = callback;
		
		var self = this;
		
		jQuery.post(this.config.url, this.config.params, function(data) {
			var result;
			if (self.config.output == 'json') {
				try {
					eval('result = ' + data);
					if (result.debug) {
						show(result.debug);
					} else if (result.forbidden) {
						show(result.forbidden);
					} 
				} catch(err) {}
			} else {
				result = data;
			}
			self.callback(result, self.config);
		});
	}
});



/* Debuger ************************************************************************************************* */

var show = function(el) {
	if (console && console.log) {
		console.log(el);
	}
	return el;
};